package com.videoplatform.repository;

import com.videoplatform.entity.Video;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VideoRepository extends JpaRepository<Video, Long> {
    
    /**
     * 根据状态查找视频
     */
    List<Video> findByStatus(Video.VideoStatus status);
    
    /**
     * 根据标题模糊查询
     */
    List<Video> findByTitleContainingIgnoreCase(String title);
    
    /**
     * 按上传时间降序排列
     */
    List<Video> findAllByOrderByUploadTimeDesc();
    
    /**
     * 按观看次数降序排列（热门视频）
     */
    List<Video> findAllByOrderByViewCountDesc();
    
    /**
     * 查找指定状态的视频，按上传时间降序
     */
    List<Video> findByStatusOrderByUploadTimeDesc(Video.VideoStatus status);
    
    /**
     * 获取总视频数量
     */
    long countByStatus(Video.VideoStatus status);
    
    /**
     * 获取总文件大小
     */
    @Query("SELECT SUM(v.fileSize) FROM Video v WHERE v.status = :status")
    Long getTotalFileSize(@Param("status") Video.VideoStatus status);
}

